<?php

class Menu_controller extends CI_Controller {
    
    function Menu_controller() {
        parent::__construct();
        
        $this->load->model('Menu_model');
    }
    
    public function index() {
        $this->load->library('session');
        $data['lang'] = $this->session->userdata('lang');
        $data['member_id'] = $this->session->userdata('member_id');
        $data['firstname'] = $this->session->userdata('firstname');
        $this->load->model('Panel_model');
        $data['panel'] = $this->Panel_model->admin_menus_list();
        foreach ($data['panel'] as $key => $option) {
            $data['panel'][$key]['option'] = $this->Panel_model->admin_options_list($option['admin_panel_id']);
        }
        
        $this->load->model('Language_model');
        
        $data['route'] = 'menus';
        $data['menus'] = $this->Menu_model->menu_list();
        $data['languages'] = $this->Language_model->languages_list();
        $this->load->view('layout/admin', $data);
    }
    
    public function set() {
        $post = $this->input->post();
        
        // restaurant customization
        if(empty($post['title'])) $post['title'] = $post['name'];
        if(empty($post['parent']) && empty($post['child'])) {
            $this->Menu_model->menu_root_set($post['language'], $post['name'], $post['title']);
        } else {
            if(!empty($post['parent']) && empty($post['child']))
                $this->Menu_model->menu_set($post['parent'], $post['language'], $post['name'], $post['title']);
            else
                $this->Menu_model->menu_set($post['child'], $post['language'], $post['name'], $post['title']);
        }
        
        redirect('/router/view_menus/'.$post['language'],'refresh');
    }
    
    public function delete($menu_id) {
        $menu = $this->Menu_model->menu_get($menu_id);
        
        $this->Menu_model->menu_delete($menu_id);
        
        redirect('/router/view_menus/'.$menu['language_id'],'refresh');
    }
    
    public function update($menu_id) {
        $data = $this->input->post();

        if(!empty($data['parent']) || $data['parent'] != 0)
            $this->Menu_model->menu_update($menu_id, $data['parent'], $data['language'], $data['name'], $data['title']);
        else 
            $this->Menu_model->menu_root_update($menu_id, $data['language'], $data['name'], $data['title']);
        
        $menu = $this->Menu_model->menu_get($menu_id);
        redirect('/router/view_menus/'.$menu['language_id'],'refresh');
    }
    
    public function order($direction, $menu_id) {
        $menu = $this->Menu_model->menu_get($menu_id);
        if(!is_null($menu['parent_menu_id']))
            $menus = $this->Menu_model->sub_menu_list($menu['parent_menu_id']);
        else
            $menus = $this->Menu_model->menu_root_list();
        switch($direction) {
            case 0 : {
                if($menu['ordering'] > 1) {
                    if(!is_null($menu['parent_menu_id']))
                        $this->Menu_model->menu_order($menu_id, '+', '-');
                    else
                        $this->Menu_model->menu_root_order($menu_id, '+', '-');
                }
            } break;
            case 1 : {
                if($menu['ordering'] < count($menus)) {
                    if(!is_null($menu['parent_menu_id']))
                        $this->Menu_model->menu_order($menu_id, '-', '+');
                    else
                        $this->Menu_model->menu_root_order($menu_id, '-', '+');
                }
            } break;
        }
        
        redirect('/router/view_menus/'.$menu['language_id']);
    }
    
    // restaurant customization
    public function set_menu_box() {
        $data = $this->input->post();
        
        $menu_id = $this->Menu_model->menu_root_set($data['language'], $data['name'], $data['name']);
        
        $this->load->library('session');
        $member_id = $this->session->userdata('member_id');
        $this->load->model('Content_model');
        $content_id = $this->Content_model->content_set($menu_id, $member_id, $data['language'], $data['name'], $data['name'], $data['content']);
        $this->Content_model->content_record_update($content_id, 'flag', 'bit', '00000001');
        
        redirect('/router/view_menus/'.$data['language'],'refresh');
    }    
    
    public function return_json_language_menu() {
        $data = $this->input->post();
        
        $menu = $this->Menu_model->menu_language_list($data['language']);
        
        echo json_encode($menu);
        return;
    }
    
}

?>
